﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EFGhh.chapater3.Model;

namespace EFGhh.chapater3
{
    public partial class Detail : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //显示传递过来的数据
                if (Request.QueryString["id"] != null)
                {
                    //获取传递过来的ID
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    using (FAQDBEntities db = new FAQDBEntities())
                    {
                        var data = db.Topics.FirstOrDefault(p => p.TopicId == id);
                        this.lblcontent.Text = data.Content;
                        this.lbltime.Text = Convert.ToDateTime(data.UpdateTime).ToString("yyyy年MM月dd日");
                        this.lblipadress.Text = data.IPAddress;
                        this.lbltitle.Text = data.Title;
                    }
                }
                using (FAQDBEntities db = new FAQDBEntities())
                {
                    //获取传递过来的ID
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    var res = db.Comments.Where(p => p.TopicId == id).ToList();
                    this.rp_question.DataSource = res;
                    this.rp_question.DataBind();
                }
            }
        }

        protected void btn_Deltrouble_Command(object sender, CommandEventArgs e)
        {
            using (FAQDBEntities db = new FAQDBEntities())
            {
                //获取传递过来的id
                int id = Convert.ToInt32(Request.QueryString["id"]);
                //找到两张表传递过来的ID
                string sql = $"delete from Topics where TopicId='{id}'";
                string delsql = $"delete from Comments where TopicId='{id}'";
                //执行删除外键，再删除主键
                db.Database.ExecuteSqlCommand(delsql, sql);
                //找到回答表的id并将对应问题的回答删除
                var res = db.Comments.Where(p => p.TopicId == id).ToList();
                //重新给回答绑定数据源
                this.rp_question.DataSource = res;
                rp_question.DataBind();
                db.Database.ExecuteSqlCommand(sql);
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功！')", true);
                Response.Redirect("Index.aspx");
            }
        }

        protected void btn_del_Command(object sender, CommandEventArgs e)
        {
            int CommentId = Convert.ToInt32(e.CommandArgument);
            using (FAQDBEntities db = new FAQDBEntities())
            {
                var data = db.Comments.FirstOrDefault(p => p.CommentId == CommentId);
                db.Comments.Remove(data);
                if (db.SaveChanges() > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('删除成功！')", true);
                    //获取传递过来的ID
                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    var res = db.Comments.Where(p => p.TopicId == id).ToList();
                    this.rp_question.DataSource = res;
                    this.rp_question.DataBind();
                }
            }
        }

        protected void btn_add_Command(object sender, CommandEventArgs e)
        {
            
        }

        protected void btn_fanhui_Click(object sender, EventArgs e)
        {
            Response.Redirect("Index.aspx");
        }
        private void Show()
        {
            using (FAQDBEntities db = new FAQDBEntities())
            {
                //获取传递过来的ID
                int id = Convert.ToInt32(Request.QueryString["id"]);
                var res = db.Comments.Where(p => p.TopicId == id).ToList();
                this.rp_question.DataSource = res;
                this.rp_question.DataBind();
            }
        }
        protected void btn_add_Click(object sender, EventArgs e)
        {
            //获取传递过来的ID
            int id = Convert.ToInt32(Request.QueryString["id"]);
            //实例化Comments对象
            Comment c = new Comment()
            {
                TopicId = id,
                Content = this.tb_response.Text,
                IPAddress = "::1",
                UpdateTime = DateTime.Now
            };
            using (FAQDBEntities db = new FAQDBEntities())
            {
                db.Comments.Add(c);
                if (db.SaveChanges() > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('添加成功！！')", true);
                    Show();
                }
            }
        }
    }
}